package com.shuqi.model;

import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.database.model.BookMarkInfo;
import com.shuqi.database.model.UserInfo;
import com.shuqi.model.bean.BookData;
import com.shuqi.model.manager.BookCatalogManager;
import com.shuqi.model.manager.MyAccountManager;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.listener.HttpRequestListener;

/**
 * 书籍封面数据
 * 
 * @author yanghuawei
 * 
 */
public class BookCatalogModel extends ModelBase implements HttpRequestListener {
    private final String logTag = "BookCatalogModel";
    private HttpRequest httpRequest;
    private UserInfo userInfo;


    public BookCatalogModel(Context context) {
        userInfo = MyAccountManager.getInstance(context);
    }

    @Override
    public void action(int actionCode, Object object) {
        Log4an.e(logTag, "action:" + actionCode);
        fireEvent(actionCode, object);
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        return null;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getBookCoverUrl("", "");
    }

    @Override
    public void onDestroy() {
        if (httpRequest != null) {
            httpRequest.abort();
        }
    }

    @Override
    public void loadData(final Object... objs) {
        final String bookId = (String) objs[0];


        Log4an.e(logTag, "loadData() bookId=" + bookId);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                BookData bookData =
                        BookCatalogManager.getBookCatalogByBidAndSid(bookId, "",
                                userInfo.getUserId(), BookMarkInfo.TYPE_NEW_PAY_BOOKMARK);
                fireEvent(-1, bookData);
            }
        };
        MyTask.runInBackground(runnable, true);
    }
}
